/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlcommand;

import data.Cell;
import data.Row;
import java.util.ArrayList;

/**
 *
 * @author Quang
 */
public class SingleCondition extends JConditions{


    private String columnName;
    private String leftPart;
    private String rightPart;
    private String operator;
    private String sql;
    private int nTable;
    private String tableName;

    public SingleCondition(String colString, String operator, String right) {
        columnName = colString;
        rightPart = right;
        this.operator = operator;
    }
    public SingleCondition(int numberTable, String colString, String operator, String right) {
         columnName = colString;
        rightPart = right;
        this.operator = operator;
        nTable = numberTable;
    }
    public SingleCondition(){
       
    }
    public void setConndition(String left, String right, String op){
        leftPart = left;
        rightPart = right;
        operator = op;
    }
    public SingleCondition(String strSQL){
        sql = strSQL;
    }
    public SingleCondition(int nTable){
        this.nTable = nTable;
        
    }
  

    public Boolean compare(MyError err) {
        // neu ca 2 la so

        int index = -1;
        if ("<".equals(getOperator())) {
            index = 0;
        }
        if ("<=".equals(getOperator())) {
            index = 1;
        }
        if (">".equals(getOperator())) {
            index = 2;
        }
        if (">=".equals(getOperator())) {
            index = 3;
        }
        if ("<>".equals(getOperator())) {
            index = 4;
        }
        if ("=".equals(getOperator())) {
            index = 5;
        }
        

        if (isNumber(leftPart) && isNumber(getRightPart())) {
            int op1 = Integer.parseInt(leftPart);
            int op2 = Integer.parseInt(rightPart);

            switch (index) {
                case 0:
                    return op1 < op2;
                case 1:
                    return op1 <= op2;
                case 2:
                    return op1 > op2;
                case 3:
                    return op1 >= op2;
                case 4:
                    return op1 != op2;
                case 5:
                    return op1 == op2;
                case 7:
                    return op1 < op2;
                default: {
                    err.setError("Cannot excute this operator in number!!!");
                    return false;
                }
            }

        } else {
            switch (index) {
                case 4:
                    return !leftPart.equals(rightPart);
                case 5:
                    return leftPart.equals(rightPart);
                default: {
                    err.setError("Cannot excute this operator in text!!!");
                    return false;
                }
            }
        }

    }
    
    @Override
    public Boolean InitValue(MyError err){
        return true;
    }
    @Override
    public Boolean compare(Row row, MyError err){
        ArrayList<Cell> cells = new ArrayList<>();
        cells = (ArrayList<Cell>) row.getListCell();
        for(int i=0; i<cells.size(); i++){
            if(cells.get(i).getName().equals(getColumnName())){
                leftPart = Command.standardizeString(cells.get(i).getData());
                Boolean result = compare(err);
                if(err.getError() ==null){
                    return result;
                }
                else
                    return false;                
            }
        }
        return false;
    }
    /**
     * so sanh gia tri tai 2 bang vi du
     * SELECT HOCSINH.TEN
     * FROM HOCSINH, LOP
     * WHERE HOCSINH.TUOI >'12' AND LOP.MALOP = '7A'
     * =>lay ten cua cac hoc sinh lon 12 tuoi cua lop 7A
     * @param row
     * @param err
     * @return 
     */
    public Boolean compareFor2Table(Row row, MyError err){
         ArrayList<Cell> cells = new ArrayList<Cell>();
        cells = (ArrayList<Cell>) row.getListCell();
        for(int i=0; i<cells.size(); i++){
            if(cells.get(i).getName().equals(getColumnName()) &&
                    cells.get(i).getTableName().equals(tableName)){
                leftPart = Command.standardizeString(cells.get(i).getData());
                Boolean result = compare(err);
                if(err.getError() ==null){
                    return result;
                }
                else
                    return false;                
            }
        }
        return false;
    }
    public Boolean isNumber(String s) {
        try {
            Integer.parseInt(s);
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }
    public JConditions String2Condition(String strCondition, MyError err){
        strCondition = Command.standardizeString(strCondition);
        int posOperator = 0;
        //String strOperator=null;
        int lenOperator = 0;
//        String strColName =null;
//        String strValues;
//        String strTableName;
        Boolean flag = false;//kiem tra co ton tai toan tu nao k?
        // lay cac toan tu
        for(int i=0; i<keyWordOperator.length; i++){
            if(strCondition.indexOf(keyWordOperator[i]) != -1)
            {
                flag = true;
                posOperator = strCondition.indexOf(keyWordOperator[i]);
                lenOperator = keyWordOperator[i].length();
                if(strCondition.charAt(posOperator+2)=='='&& lenOperator ==1){
                    operator = keyWordOperator[i] + '=';
                    lenOperator =3;
                    break;
                }
                else if(strCondition.charAt(posOperator+2)=='>'&& lenOperator ==1){
                    operator = keyWordOperator[i] + '>';
                    lenOperator =3;
                    break;
                }
                
                else 
                    operator = keyWordOperator[i];
                            
                
            }
        }
        if(!flag)
            return null;
        
        // lay cac gia tri
        try{
            if(1 == nTable){
                columnName = strCondition.substring(0, posOperator-1);
            }else if(2== nTable){
                int posDot = strCondition.indexOf(".");     
                if(posDot <= 0){// ko co dau cham
                    err.setError("Error!! Phai co ten bang truoc ten cot! ");
                    return null;
                    
                    
                }
                tableName = strCondition.substring(0,posDot);
                columnName = strCondition.substring(posDot+1, posOperator-1);
            }
            rightPart = strCondition.substring(posOperator + lenOperator+1,strCondition.length());
            StringBuilder str = new StringBuilder(Command.standardizeString(rightPart));
            if(str.charAt(0) == '\'')
                str.deleteCharAt(0);
            if(str.charAt(str.length()-1) == '\'')
                str.deleteCharAt(str.length()-1);
            rightPart = str.toString();
            return this;
        }catch(Exception e){
            return null;
        }
        
    }

  
    /**
     * @return the rightPart
     */
    public String getRightPart() {
        return Command.standardizeString(rightPart);
    }

    /**
     * @param rightPart the rightPart to set
     */
    public void setRightPart(String rightPart) {
        this.rightPart = rightPart;
    }

    /**
     * @return the operator
     */
    public String getOperator() {
        return Command.ChuanHoa(operator);
    }

    /**
     * @param operator the operator to set
     */
    public void setOperator(String operator) {
        this.operator = operator;
    }

    /**
     * @return the columnName
     */
    public String getColumnName() {       
       return Command.standardizeString(columnName);
    }

    /**
     * @param columnName the columnName to set
     */
    public void setColumnName(String columnName) {
        this.columnName = Command.standardizeString(columnName);
    }
}
